package lib;

public class ArrayManipulation {

	static public void printArray(double[] b){
		for(int i=0;i<b.length;i++){
			System.out.print(b[i]+"\t");
		}
		System.out.println();
	} 
	static public void printArray(String[] b){
		for(int i=0;i<b.length;i++){
			System.out.print(b[i]+"\t");
		}
		System.out.println();
	}
	static public void printArrayT(double[][] b){
		for(int i=0;i<b[0].length;i++){
			
			for(int j=0;j<b.length;j++){
				System.out.print(b[j][i]+"\t");
			}
			System.out.println();
			
		}
		System.out.println();
	}
	static public void printArray(double[][] b){
		for(int i=0;i<b.length;i++){
			
			for(int j=0;j<b[0].length;j++){
				System.out.print(b[i][j]+"\t");
			}
			System.out.println();
			
		}
		 System.out.println();
	}
	static public double[][] zeroArray(double[][] b){
		for(int i=0;i<b.length;i++){
			for(int j=0;j<b[0].length;j++){
				b[i][j]=0;
			}
		}
		return b;
	}
	
	static public double[][] test_zeroArray(double[][] b){
		for(int i=0;i<b.length;i++){
			for(int j=0;j<b[0].length;j++){
				b[i][j]=0;
			}
		}
		return b;
	}
	static public double[] zeroArray(double[] b){
		for(int i=0;i<b.length;i++){
			b[i]=0;
		}
		return b;
	}
	static public double[] fillArray(double[] b,double a){
		for(int i=0;i<b.length;i++){
			b[i]=a;
		}
		return b;

	}

	static public double[][] fillArray(double[][] b,double a){
		for(int i=0;i<b.length;i++){
			for(int j=0;j<b[0].length;j++){
				b[i][j]=a;
			}
		}
		return b;
	}

	
	
	//Descending the Array b according to b[0];
	static public double[][] descendArray(double[][] b){
		//printArray(b);
		//****************************************************
		//rerank the matrix according to the b[0]; 
	
		for(int i=1;i<b.length;i++){
			double[] m=new double[b[0].length];
			for(int p=0;p<b[0].length;p++){
				m[p]=b[i][p];
			}
			for(int j=0;j<i;j++){
				if(m[0]>b[j][0]){
					for(int k=i;k>j;k--){
						for(int q=0;q<b[0].length;q++){
							b[k][q]=b[k-1][q];
						}
					}
					for(int q=0;q<b[0].length;q++){
						b[j][q]=m[q];
					}
					break;
				}
			}
		}
		//printArray(b);
		
		//******************************************************
		
		//rerank the matrix acooding to the b[0]+b[4]
/*		
		for(int i=1;i<b[0].length;i++){
			double[] m=new double[b.length];
			for(int p=0;p<b.length;p++){
				m[p]=b[p][i];
			}
			for(int j=0;j<i;j++){
				if(m[0]*m[4]>b[0][j]*b[4][j]){
					for(int k=i;k>j;k--){
						for(int q=0;q<b.length;q++){
							b[q][k]=b[q][k-1];
						}
					}
					for(int q=0;q<b.length;q++){
						b[q][j]=m[q];
					}
					break;
				}
			}
		}
		
	//**************************************************************
*/
		
		return b;
	}
	static public double[][] ascendArray(double[][] b){
		//printArray(b);
		//****************************************************
		//rerank the matrix according to the b[0]; 
	
		for(int i=1;i<b.length;i++){
			double[] m=new double[b[0].length];
			for(int p=0;p<b[0].length;p++){
				m[p]=b[i][p];
			}
			for(int j=0;j<i;j++){
				if(m[0]<b[j][0]){
					for(int k=i;k>j;k--){
						for(int q=0;q<b[0].length;q++){
							b[k][q]=b[k-1][q];
						}
					}
					for(int q=0;q<b[0].length;q++){
						b[j][q]=m[q];
					}
					break;
				}
			}
		}
		
		return b;
	}
	
	/*
	 * thrim the '0' or '0.0' the 'd' array contained, this useless num is emerged when the array initiated
	 */
	static public double[] array_trim(double[] d){
		
		int i;
		for(i=d.length-1;i>=0;i--){
			if(d[i]!=0) break;
		}
		double[] trimArray = new double[i+1];
		for(;i>=0;i--){
			trimArray[i]=d[i];
		}
		return trimArray;
	}
	
	static public double[][] array_trim(double[] d1,double[] d2){
		int nonzero = d1.length;
		
		for(int i=0;i<d1.length;i++){
			if(d1[i]==0&&d2[i]==0) nonzero--;
		}
		double[][] trimArray = new double[2][nonzero];
		
		for(int i=d1.length-1;i>=0;i--){
			if(d1[i]==0&&d2[i]==0){
			}
			else{
				trimArray[0][nonzero-1]=d1[i];
				trimArray[1][nonzero-1]=d2[i];
				nonzero--;
			}
		}

		return trimArray;
	}

	
	static public double[][] array_del_zero(double[][] d){
		/*
		 * for array double[n][2]. double[n][0] contains  the position information; 
		 * double[n][1] contains the intensity information 
		 * [2] could be [3],...[i];
		 * this function is del d[i] whose intensity is 0;
		 */
		
		int non_zero_num=0;
		for(int i=0;i<d.length;i++){
			boolean b = false;
			for(int j=1;j<d[0].length;j++){
				if(d[i][j]!=0){
					b = true;
				}
			}
			if(b) non_zero_num++;
		}
		//change the array 
		//double[][] trimArray = new double[non_zero_num][d[0].length];
		double[][] trimArray = new double[non_zero_num][2];
		int index =0 ;
		for(int i=0;i<d.length;i++){
			boolean b = false;
			for(int j=1;j<d[0].length;j++){
				if(d[i][j]!=0){
					b = true;
				}
			}
			if(b){
				for(int j=0;j<d[0].length;j++){
					trimArray[index][j]=d[i][j];
				}
				index++;
			}
		}
		return trimArray;
	}

	
	//merge array a and b to one array;
	static public double[][] array_merge(double[][] a,double[][] b){
		if(a==null||a.length==0) return b;
		if(b==null||b.length==0) return a;
		
		if(a[0].length!=b[0].length){
			
			printArray(a);
			System.out.println("The dimension does not pair!!! " +a[0].length+"\t"+b[0].length);
			printArray(b);
			return new double[0][0];
		}
		double[][] m = new double[a.length+b.length][a[0].length];
		int i=0;
		for(;i<a.length;i++){
			for(int j=0;j<a[0].length;j++){
				m[i][j]=a[i][j];
			}
		}
		for(;i<m.length;i++){
			for(int j=0;j<b[0].length;j++){
				m[i][j]=b[i-a.length][j];
			}
		}
		return m;
	}
	static public double sum_array(double[] d){
		double sum=0;
		int i=0;
		for(;i<d.length;i++){
			sum+=d[i];
		}
		return sum;
	}
	static public double [] div_array(double[] d, double v){
		int i=0;
		for(;i<d.length;i++){
			d[i]=d[i]/v;
		}
		return d;
	}
	static public double [] descendArray_insert(double[] d, double v){
		int i=0;
		for(;i<d.length;i++){
			if(v>d[i]) break;
		}
		for(int j=d.length-1;j>i;j--){
			d[j]=d[j-1];
		}
		if(i==d.length) i--;
		d[i]=v;
		return d;
	}
	static public double [][] descendArray_insert(double[][] d, double[] v){
		if(d[0].length!=v.length){
			System.out.println("Sorry, the format of the input data is wrong!");
			return null;
		}
		int i=0;
		for(;i<d.length;i++){
			if(v[0]>d[i][0]) break;
		}
		for(int j=d.length-1;j>i;j--){
			for(int s=0;s<v.length;s++){
				d[j][s]=d[j-1][s];
			}
		}
		if(i==d.length) i--;
		for(int s=0;s<v.length;s++){
			d[i][s]=v[s];
		}
		
		return d;
	}	
	
	static public double[] descendArray(double[] b){
		//rerank the vector; 
	
		for(int i=1;i<b.length;i++){
			double m=b[i];
			for(int j=0;j<i;j++){
				if(m>b[j]){
					for(int k=i;k>j;k--){
							b[k]=b[k-1];
					}
						b[j]=m;
					break;
				}
			}
		}
		
		return b;
	}

	static public double get_Zhang_similarity(double[] d1,double[] d2){
		double squareD1=0;
		double squareD2=0;
		for(int i=0;i<d1.length;i++){
			squareD1 +=d1[i];
			squareD2 +=d2[i];
		}
		//double k=squareD2/squareD1;
		double convolution=0;
		double denominator=Math.sqrt(squareD1)*Math.sqrt(squareD2);
		for(int i=0;i<d1.length;i++){
			convolution += Math.sqrt(d1[i]*d2[i]);
			//denominator +=(k*d1[i]+d2[i])/2;
		}
		return convolution/denominator;

	}
	static public double[][] transpose(double[][] d){
		if(d==null||d.length==0) return null;
		double[][] tempd = new double[d[0].length][d.length];
		for(int i=0;i<d.length;i++){
			for(int j=0;j<d[0].length;j++){
				tempd[j][i]=d[i][j];

			}
		}
		return tempd;
	}	

}

